package com.android.jiradroid;

import java.util.Iterator;
import java.util.Map;

import com.android.jiradroid.R;

import android.app.Activity;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.content.Intent; 
import android.content.SharedPreferences;
import android.view.MenuInflater;
import android.widget.TextView;
import android.app.Dialog;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.MarshalFloat;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

public class JIRAdroid extends Activity {
	private static final int DIALOG_ABOUT = 1;
	private static final int REQUEST_CODE_PREFERENCES = 1;
	private Preference mPreference;
	TextView mTextView;
	
    /** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	    setContentView(R.layout.main);
	    JIRAlogin();
	}
    
	@Override
	protected final Dialog onCreateDialog(final int id) {
		Dialog d;

		switch (id) {
		case DIALOG_ABOUT:
			d = new Dialog(this);
			d.setContentView(R.layout.about);
			d.setTitle(this.getString(R.string.app_name) + " v" + this.getString(R.string.app_version));
			return d;
		default:
			return null;
		}
	}
    
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = this.getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
		return true;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		/* after setup */
		if (requestCode == REQUEST_CODE_PREFERENCES) {
			JIRAlogin();
		}
	}

	private void JIRAlogin() {
		String msg = "JIRAdroid Preference\n";

		SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
		
		Map myPref = sharedPref.getAll();
		Iterator itor = myPref.entrySet().iterator();
		while (itor.hasNext()) 	{
			Map.Entry mEntry = (Map.Entry)itor.next();
			msg += mEntry.getKey() + "=" + mEntry.getValue() + "\n";
		}
		
		String username = sharedPref.getString("username", "");
		String password = sharedPref.getString("password", "");
		String jiraurl = sharedPref.getString("jiraurl", "");

		if (username != "" && password != "" && jiraurl != "") {
			SoapObject rpc = new SoapObject("x", "login");
			
			msg += "\nTrying to connect JIRA server...\n";
			
			rpc.addProperty("username", username);
			rpc.addProperty("password", password);
			jiraurl += "/rpc/soap/jirasoapservice-v2?wsdl";
		
			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
			envelope.setOutputSoapObject(rpc);
			new MarshalFloat().register(envelope);
		
			HttpTransportSE transportSE = new HttpTransportSE(jiraurl);
			transportSE.debug  = true;
		
			try {
				// call and print out the result
				transportSE.call("login", envelope);
				msg += "Login Token=" + envelope.getResponse();
			} catch (Exception e) {
				e.printStackTrace();
				msg += e.getMessage();
				//System.out.println(transportSE.responseDump);
			}
		} else {
			msg += "JIRA Account not configured.\n";
		}
		
		mTextView = (TextView) findViewById(R.id.TextView01);
		mTextView.setText(msg);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.refresh:
			JIRAlogin();
			return true;
		case R.id.setup:
			Log.d("JIRAdroid", "Enter Setup Screen");
			startActivityForResult(new Intent(this, Preferences.class), REQUEST_CODE_PREFERENCES);
			return true;
//		case R.id.quit:
//			this.finish();
//			return true;
		case R.id.about:
			this.showDialog(DIALOG_ABOUT);
			return true;
		default:
			return false;
		}
	}
}

